﻿#include <iostream>
#include <string>
#include <curl.h>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
size_t write_callback(void* ptr, size_t size, size_t nmemb, void* stream) {
	return fwrite(ptr, size, nmemb, (FILE*)stream);
}
int download_file(const char* url, const char* output_filename) {
	CURL* curl = curl_easy_init();
	FILE* fp = fopen(output_filename, "wb"); // 打开⼀个⽂件⽤于保存下载的数据
	if (curl && fp) {
		// 设置下载选项
		curl_easy_setopt(curl, CURLOPT_URL, url);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
		// 执⾏下载请求
		CURLcode res = curl_easy_perform(curl);
		if (res != CURLE_OK) {
			fprintf(stderr, "下载失败: %s\n", curl_easy_strerror(res));
			fclose(fp);
			curl_easy_cleanup(curl);
			return -1;
		}
		fclose(fp);
	}else {
		fprintf(stderr, "初始化失败\n");
		if (fp) {
			fclose(fp);
		}
		if (curl) {
			curl_easy_cleanup(curl);
		}
		return -1;
	}
	curl_easy_cleanup(curl);
	return 0;
}
string ConvertUTF8toGBK(const string& str) {
	int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
	std::wstring wstr(len, L'\0');
	MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], len);
	len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
	std::string ret(len, '\0');
	WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &ret[0], len, NULL, NULL);
	return ret;
}
bool directory_exists(const fs::path& dir_path) {
	return fs::exists(dir_path) && fs::is_directory(dir_path);
}
int main() {
	fs::path dir_path = "/path/to/directory";
	if (directory_exists(dir_path)) {
		std::cout << "Directory exists." << std::endl;
	}else {
		std::cout << "Directory does not exist." << std::endl;
	}
	return 0;
}